<template>
  <div>
    <hy-order-descriptions :orderId='orderId' v-if='loaded'></hy-order-descriptions>
    <a-descriptions bordered size='small' :column='4' title='退货单信息'  style='margin-top: 20px' v-if='backOrderForm.outOrderNo'>
      <a-descriptions-item label='订单编号'>
        {{ backOrderForm.orderNo }}
      </a-descriptions-item>
      <a-descriptions-item label='退货单号'>
        {{ backOrderForm.outOrderNo }}
      </a-descriptions-item>
      <a-descriptions-item label='退货日期'>
        {{ backOrderForm.sendDate }}
      </a-descriptions-item>
      <a-descriptions-item label='订单状态'>
        <a-tag color='#87d068' v-if='backOrderForm.orderState==0'>未提交</a-tag>
        <a-tag color='#87d068' v-if='backOrderForm.orderState==1'>{{ backOrderForm.taskName }}</a-tag>
        <a-tag color='#f50' v-if='backOrderForm.orderState==2'>已审批</a-tag>
      </a-descriptions-item>
      <a-descriptions-item label='备注' :span='2' v-if='backOrderForm.remark'>
        {{ backOrderForm.remark }}
      </a-descriptions-item>
    </a-descriptions>
    <a-divider orientation='left' v-if='backOrderForm.outOrderNo'>退货信息</a-divider>
    
    <a-divider orientation='left' v-else>发货信息</a-divider>
    <f-table ref='orderDrugs' rowKey='orderDetailsId' 
             :showTotal='true' 
             :tableColumns='orderDetailsColumns'
             :scroll='{x:true}' 
             handler='hyOrderReturnHandler'
             :operator='operator' 
             :params='queryParam' 
             :api='api.hyOrderDetails' :autoLoad='false' 
             :showPagination='false'
             :multiple='false'>
      <span slot='drugType' slot-scope='text, record, index'>
        <a-tag color='#87d068' v-if='text=="ZS"'>招商</a-tag>
        <a-tag color='#108ee9' v-if='text=="ZY"'>自营</a-tag>
      </span>
      <span slot='saleType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text=="CHA"'>销售</a-tag>
           <a-tag color='#FF0000' v-if='text=="CHC"'>退货</a-tag>
           <a-tag color='purple' v-if='text=="PZ"'>票折</a-tag>
       </span>
      <span slot='settleAmountD' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.settleFormulaContentD'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='settleAmountE' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.settleFormulaContentE'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='settleAmountC' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.settleFormulaContentC'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='settleAmount' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.settleFormulaContent'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='taxRateDiff' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.taxRateDiffFormula'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='deptProfitAmount' slot-scope='text, record, index'>
        <a-tooltip placement='top' :title='record.deptProfitAmountFormula'> <span>{{ text }}</span></a-tooltip>
      </span>
      <span slot='action' slot-scope='text, record,index' v-if='(calculate && record.rowIndex!="合计") || (orderForm.calculateState!=2 && record.rowIndex!="合计")'>
        <template>
           <a-button type='primary' @click='$refs.calcuateDrawer.showDrawer(record,true,orderForm.deptType)' size='small'>核算</a-button>
        </template>
        </span>
    </f-table>
    <hy-order-calcuate-drawer ref='calcuateDrawer' @success='onLoad'></hy-order-calcuate-drawer>
  </div>
</template>

<script>
import api from "../../../js/api"
import HyOrderCalcuateDrawer from "@/views/module/haiying/order/caclulate/hy_order_calculate_drawer"
import { commonMixins } from "@/core/mixins/commonMixins"
import HyOrderDescriptions from "@/views/module/haiying/order/details/hyorder_descriptions"


export default {
  name: "CaclulateApproveDetails",
  components: { HyOrderDescriptions, HyOrderCalcuateDrawer },
  props: {
    allowHs: {
      type: Boolean,
      default: false
    }
  },
  mixins: [commonMixins],
  data() {
    let me = this
    //操作按钮
    let operator = []
    let isCw = this.$store.state.user.roleInfo.isCw
    let isSw = this.$store.state.user.roleInfo.isSw
    let calculate = this.$store.state.user.roleInfo.calculate
    let isHs = this.$store.state.user.roleInfo.isHs
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      processImageVisible: false,
      formItem: this.getFormItem(),
      orderForm: {},
      backOrderForm: {},
      orderId: null,
      loaded: false,
      calculate: calculate || isHs,
      //是否是财务角色
      isCw: isCw,
      isSw: isSw,
      operator: operator,
      orderDetailsColumns: [],
      customHeaderCell: (column) => {
        return {
          style: {
            "backgroundColor": "#722ED1",
            color: "red"
          }
        }
      }
    }
  },
  created() {
  },
  computed: {
    title: function() {
      return this.saleType == "CHC" ? "退货" : "补货"
    }
  },
  methods: {
    getFormItem() {
      return {
        returnId: "",
        saleNum: 0,
        noReturnNum: 0,
        noReturnAmount: "",
        sendDate: "",
        hkImage: []
      }
    },
    getColumns1(data) {
      let deptType = data.deptType
      let sendType = data.sendType
      let rebateState = data.rebateState
      let saleManId = !data.saleManId
      let saleManIdD = !data.saleManIdD
      let saleManIdC = !data.saleManIdC
      let saleManIdE = !data.saleManIdE
      let hiddenDept = !(this.isCw || this.isSw)
      let calculate = this.calculate && data.calculateState != 2
      let isCw = this.isCw
      let isSw = this.isSw
      let hiddenSj = true
      if (sendType == 3 && isCw == true) {
        hiddenSj = false
      }
      if (this.superAdmin) {
        calculate = true
      }
      if (this.allowHs == true) {
        calculate = true
      }
      return [
        {
          title: "操作",
          align: "center",
          fixed: "left",
          hidden: !calculate,
          dataIndex: "action",
          scopedSlots: { customRender: "action" }
        },
        {
          title: "商品名称",
          align: "left",
          ellipsis: true,
          width: "140px",
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          ellipsis: true,
          length: 40,
          width: "200px",
          dataIndex: "drugsSpec"
        },
        {
          title: "批号",
          align: "left",
          ellipsis: true,
          width: "100px",
          dataIndex: "drugLotNo"
        },
        {
          title: "销售类型",
          align: "center",
          width: "80px",
          dataIndex: "saleType",
          scopedSlots: { customRender: "saleType" }
        },
        {
          title: "商品类型",
          align: "center",
          width: "80px",
          dataIndex: "drugType",
          scopedSlots: { customRender: "drugType" }
        },
        {
          title: "销售单价",
          align: "center",
          width: "80px",
          dataIndex: "salePrice"
        },
        {
          title: "数量",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "saleNum"
        },
        {
          title: "销售金额",
          align: "center",
          width: "80px",
          needTotal: true,
          dataIndex: "saleAmount"
        },
        {
          title: "成本单价",
          align: "center",
          width: "80px",
          hidden: !this.isCw,
          dataIndex: "costPrice"
        },
        {
          title: "成本金额",
          align: "center",
          width: "80px",
          hidden: !this.isCw,
          dataIndex: "costAmount"
        },
        {
          title: "高开税金",
          align: "center",
          width: "90px",
          hidden: sendType != 3,
          dataIndex: "customGapUpAmount"
        },
        {
          title: "资保金应收",
          align: "center",
          width: "90px",
          hidden: sendType != 3,
          dataIndex: "customBond"
        },
        {
          title: "部门",
          align: "center",
          width: "90px",
          hidden: deptType == "ZS",
          children: [
            {
              title: "结算价",
              align: "center",
              width: "80px",
              dataIndex: "deptPrice"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              dataIndex: "deptRate"
            },
            {
              title: "部门费用",
              align: "center",
              width: "90px",
              dataIndex: "deptSettleAmount"
            }
          ]
        },
        {
          title: "渠道商",
          align: "center",
          width: "120px",
          hidden: saleManId,
          children: [
            {
              title: "结算价",
              align: "center",
              width: "80px",
              hidden: saleManId,
              dataIndex: "settlePrice"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              hidden: saleManId,
              dataIndex: "settleRate"
            },
            {
              title: "发货结算金额",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: !this.isCw,
              dataIndex: "settleSendAmount"
            }, {
              title: "结算高开税金",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: hiddenSj,
              dataIndex: "customGapUpAmount"
            },
            {
              title: "提成金额",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: rebateState == 0,
              dataIndex: "settleRebateAmount"
            },
            {
              title: "结算金额",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: saleManId,
              scopedSlots: { customRender: "settleAmount" },
              dataIndex: "settleAmount"
            }
          ]
        },
        {
          title: "商务经理",
          align: "center",
          width: "120px",
          hidden: saleManIdD,
          children: [
            {
              title: "结算价",
              align: "center",
              width: "80px",
              dataIndex: "settlePriceD"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              dataIndex: "settleRateD"
            },
            {
              title: "结算金额",
              align: "center",
              width: "90px",
              needTotal: true,
              scopedSlots: { customRender: "settleAmountD" },
              dataIndex: "settleAmountD"
            }
          ]
        },
        {
          title: "片区服务商",
          align: "center",
          width: "120px",
          hidden: saleManIdC,
          children: [
            {
              title: "结算价",
              align: "center",
              width: "90px",
              dataIndex: "settlePriceC"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              dataIndex: "settleRateC"
            },
            {
              title: "结算金额",
              align: "center",
              width: "90px",
              needTotal: true,
              scopedSlots: { customRender: "settleAmountC" },
              dataIndex: "settleAmountC"
            }
          ]
        },
        {
          title: "商务总负责人",
          align: "center",
          hidden: saleManIdE,
          children: [
            {
              title: "结算价",
              align: "center",
              width: "80px",
              dataIndex: "settlePriceE"
            },
            {
              title: "结算标准",
              align: "center",
              width: "90px",
              dataIndex: "settleRateE"
            },
            {
              title: "结算金额",
              align: "center",
              width: "90px",
              scopedSlots: { customRender: "settleAmountE" },
              dataIndex: "settleAmountE"
            }
          ]
        },
        {
          title: "利润",
          align: "center",
          children: [
            {
              title: "超标准差额",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: !(isCw || isSw),
              scopedSlots: { customRender: "taxRateDiff" },
              dataIndex: "taxRateDiff"
            },
            {
              title: "部门利润",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: !(isCw || isSw),
              scopedSlots: { customRender: "deptProfitAmount" },
              dataIndex: "deptProfitAmount"
            },
            {
              title: "公司利润",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: !isCw,
              dataIndex: "profitAmount"
            },
            {
              title: "税收利润",
              align: "center",
              width: "90px",
              hidden: !isCw,
              needTotal: true,
              dataIndex: "taxProfitAmount"
            },
            {
              title: "产品利润",
              align: "center",
              width: "90px",
              needTotal: true,
              hidden: !isCw,
              dataIndex: "drugProfitAmount"
            },
            {
              title: "边际利润",
              align: "center",
              width: "90px",
              hidden: !isCw,
              needTotal: true,
              dataIndex: "bjProfitAmount"
            },
            {
              title: "支付开票税金",
              align: "center",
              width: "100px",
              hidden: !isCw,
              needTotal: true,
              dataIndex: "payInvoiceTaxAmount"
            }
          ]
        }
      ]
    },
    setData(data) {
      if (data && data.orderId) {
        let params = {
          orderId: data.orderId,
          handlerName: "hyOrderGetHandler"
        }
        this.http.get(this, {
          url: this.api.hyorder.get,
          params: params,
          success: (me, data) => {
            this.orderForm = data
            let orderType=data.orderType
            if(orderType=="CHC"){
              this.orderId = data.parentId
              this.getBackData(data.orderId);
            }else{
              this.orderId = data.orderId
            }
            this.loaded = true
          }
        })
        this.queryParam.orderId = data.orderId
        this.order = data
        this.orderDetailsColumns = this.getColumns1(data)
        this.$refs["orderDrugs"].refresh(false)
      }
    },
    getBackData(orderId){
      let params = {
        orderId: orderId,
        handlerName: "hyOrderGetHandler"
      }
      this.http.get(this, {
        url: this.api.hyorder.get,
        params: params,
        success: (me, data) => {
          this.backOrderForm = data
        }
      })
    },
    onLoad() {
      this.$refs["orderDrugs"].refresh(false)
    },
    renderTdBackground(record, flag) {
      return {
        style: {
          "background-color": "rgb(255,150,150)"
        }
      }
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    }
  }
}
</script>
<style lang='less'>


</style>